/*
 *   This File is Part of CuteOgre Game Engine
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	Copyright 2010 Alexandre Pires ( c.alexandre.pires@gmail.com )
 *  Created on: 2010/08/22
 */

#ifndef CONFIGMANAGER_H_
#define CONFIGMANAGER_H_

#include "IManager.h"
#include "Singleton.h"
#include <QString>
#include <QSettings>
#include <QScriptValue>

class QScriptContext;
class QScriptEngine;

class ConfigManager :
		public QObject,
		public IManager,
		public Singleton<ConfigManager> {

friend class GameEngine;
friend QScriptValue configSetParam(QScriptContext* rContext, QScriptEngine* rEngine);
friend QScriptValue configGetParam(QScriptContext* rContext, QScriptEngine* rEngine);

private:
	QSettings* mSettings;
	bool mShutdown;

protected:
	ConfigManager ( ) :
		mShutdown ( true ) {
	}

public:
	~ConfigManager () {
		delete mSettings;
	}

	static ConfigManager* getSingletonPtr ( void ) {
		return ms_Singleton;
	}

	static ConfigManager& getSingleton ( void ) {
		assert( ms_Singleton );
		return ( *ms_Singleton );
	}

	void init ();
	void shutdown ();

	int getInt ( const QString& rKey, int rDefaultValue, const QString& rGroup =
			"General" );

	void setInt ( const QString& rKey, int rValue, const QString& rGroup =
			"General" );

	bool getBool ( const QString& rKey, bool rDefaultValue,
			const QString& rGroup = "General" );

	void setBool ( const QString& rKey, bool rValue, const QString& rGroup =
			"General" );

	float getFloat ( const QString& rKey, float rDefaultValue,
			const QString& rGroup = "General" );

	void setFloat ( const QString& rKey, float rValue, const QString& rGroup =
			"General" );

	QString getString ( const QString& rKey, const QString& rDefaultValue,
			const QString& rGroup = "General" );

	void setString ( const QString& rKey, const QString& rValue,
			const QString& rGroup = "General" );

	bool valueExist ( const QString& rKey, const QString& rGroup );

	inline static int readInt ( const QString& rKey, int rDefaultValue, const QString& rGroup = "General" ){
		return ConfigManager::getSingleton().getInt(rKey,rDefaultValue,rGroup);
	}

	inline static void writeInt ( const QString& rKey, int rValue, const QString& rGroup = "General" ) {
		ConfigManager::getSingleton().setInt(rKey,rValue,rGroup);
	}

	inline static bool readBool ( const QString& rKey, bool rDefaultValue, const QString& rGroup = "General" ) {
		return ConfigManager::getSingleton().getBool(rKey,rDefaultValue,rGroup);
	}

	inline static void writeBool ( const QString& rKey, bool rValue, const QString& rGroup = "General" ) {
		ConfigManager::getSingleton().setBool(rKey,rValue,rGroup);
	}

	inline static float readFloat ( const QString& rKey, float rDefaultValue, const QString& rGroup = "General" ) {
		return ConfigManager::getSingleton().getFloat(rKey,rDefaultValue,rGroup);
	}

	inline static void writeFloat ( const QString& rKey, float rValue, const QString& rGroup = "General" ) {
		ConfigManager::getSingleton().setFloat(rKey,rValue,rGroup);
	}

	inline static QString readString ( const QString& rKey, const QString& rDefaultValue, const QString& rGroup = "General" ) {
		return ConfigManager::getSingleton().getString(rKey,rDefaultValue,rGroup);
	}

	inline static void writeString ( const QString& rKey, const QString& rValue, const QString& rGroup = "General" ) {
		ConfigManager::getSingleton().setString(rKey,rValue,rGroup);
	}

protected:

	QVariant getValue ( const QString& rKey, const QVariant& rDefaultValue =
			QVariant () );

	QVariant getValue ( const QString& rKey, const QString& rGroup,
			const QVariant& rDefaultValue = QVariant () );

	void setValue ( const QString& rKey, const QVariant& rValue );

	void setValue ( const QString& rKey, const QVariant& rValue,
			const QString& rGroup );

};

#endif /* CONFIGMANAGER_H_ */
